home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / mgr / sparcmgr / demo1.zoo / demo / icon / browse.c next >
Encoding:
C/C++ Source or Header  |  1989-08-24  |  8.9 KB  |  365 lines

  1. /*                        Copyright (c) 1987 Bellcore
  2.  *                            All Rights Reserved
  3.  *       Permission is granted to copy or use this program, EXCEPT that it
  4.  *       may not be sold for profit, the copyright notice must be reproduced
  5.  *       on copies, and credit should be given to Bellcore where it is due.
  6.  *       BELLCORE MAKES NO WARRANTY AND ACCEPTS NO LIABILITY FOR THIS PROGRAM.
  7.  */
  8. /*    $Header: browse.c,v 4.2 88/07/19 14:12:58 sau Exp $
  9.     $Source: /tmp/mgrsrc/demo/icon/RCS/browse.c,v $
  10. */
  11. static char    RCSid_[] = "$Source: /tmp/mgrsrc/demo/icon/RCS/browse.c,v $$Revision: 4.2 $";
  12.  
  13. /* display bitmap files on window */
  14.  
  15. #include "term.h"
  16. #include <signal.h>
  17.  
  18. #define GAP        3        /* space between icons */
  19. #define MAXICONS    500        /* max number of icons */
  20. #define SAVE        2        /* temp bitmap # */
  21.  
  22. #define dprintf        if(debug)fprintf
  23.  
  24. struct icon_pos {
  25.    int x,y;                /* position on window */
  26.    int w,h;                /* size of icon */
  27.    char *name;                /* name of icon */
  28.    };
  29.  
  30. struct icon_pos icons[MAXICONS];
  31. char line[MAXLINE];        /* input buffer */
  32. char cwd[MAXLINE];
  33. int win_x, win_y;        /* window position */
  34. int win_high, win_wide;        /* window size */
  35. int f_wide, f_high;        /* font size */
  36. int debug;
  37. char **first;            /* pntr to first icon */
  38. int invert = 0;
  39.  
  40. struct menu_entry menu1[] = {
  41.    "reread","r\r","next","n\r", "prev","p\r", "quit","q\r",
  42.    };
  43.  
  44. struct menu_entry menu2[] = {
  45.    "reread","r\r","next","n\r",    "quit","q\r"
  46.    };
  47.  
  48. struct menu_entry menu3[] = {
  49.    "reread","r\r","prev","p\r",    "quit","q\r"
  50.    };
  51.  
  52. struct menu_entry menu4[] = {
  53.    "reread","r\r","quit","q\r"
  54.    };
  55.  
  56. main(argc,argv)
  57. char **argv;
  58.    {
  59.    int page = 1;            /* current page number */
  60.    int count;                /* number of icons on page */
  61.    int x,y;                /* mouse position */
  62.    int n = -1;                /* current icon */
  63.    int clean();
  64.    char *getenv();
  65.    FILE *popen(), *fd = popen("/bin/pwd","r");
  66.  
  67.    ckmgrterm( *argv );
  68.  
  69.    debug = (int) getenv("DEBUG");
  70.  
  71.    if (fd) {
  72.       fgets(cwd,sizeof(cwd),fd);
  73.       *(cwd + strlen(cwd) - 1) = '\0';    /* blah */
  74.       dprintf(stderr,"Got cwd: [%s]\n",cwd);
  75.       pclose(fd);
  76.       }
  77.    else {
  78.       fprintf(stderr,"%s: can't get current directory\n",*argv);
  79.       exit(2);
  80.       }
  81.  
  82.    if (argc <2 || argc > MAXICONS) {
  83.       fprintf(stderr,"usage: %s icons... (up to %d)\n",*argv,MAXICONS);
  84.       exit(1);
  85.       }
  86.  
  87.    if (strcmp(argv[1],"-r")==0) {
  88.       invert++;
  89.       argv++;
  90.       argc--;
  91.       }
  92.  
  93.    first = ++argv;
  94.    argc--;
  95.  
  96.    m_setup(M_FLUSH);
  97.    m_push(P_BITMAP|P_MENU|P_EVENT|P_FLAGS);
  98.    m_ttyset();
  99.  
  100.    menu_load(1,4,menu1);
  101.    menu_load(2,3,menu2);
  102.    menu_load(3,3,menu3);
  103.    menu_load(4,2,menu4);
  104.  
  105.    signal(SIGINT,clean);
  106.    signal(SIGTERM,clean);
  107.  
  108.    m_setmode(M_ABS);
  109.    m_setmode(M_NOWRAP);
  110.    get_size(&win_x,&win_y,&win_wide, &win_high);
  111.    get_font(&f_wide, &f_high);
  112.  
  113.    m_setevent(BUTTON_1,"^%p\r");
  114.    m_setevent(BUTTON_1U,"$\r");
  115.    m_setevent(MOVE,"M\r");
  116.    m_setevent(RESHAPED,"R\r");
  117.    m_setevent(REDRAW,"r\r");
  118.  
  119.    count = fill_page(first,icons);
  120.  
  121.    if (count == 0) {
  122.       fprintf(stderr,"%s: no files in icon format\n",*--argv);
  123.       clean(3);
  124.       }
  125.  
  126.    set_menu(first-argv,argc-count-(first-argv)-1);
  127.  
  128.    dprintf(stderr,"Got %d,%d %dx%d [%dx%d]\n",
  129.            win_x, win_y, win_wide, win_high, f_wide, f_high);
  130.  
  131.    while(m_gets(line) != NULL) {
  132.       dprintf(stderr,"Got [%s]\n",line);
  133.       switch(*line) {
  134.      case 'q':
  135.         clean(0);
  136.         break;
  137.          case ':':        /* got a message - send current icon back */
  138.             sscanf(line+1,"%d",&x);
  139.             if (n >= 0) {
  140.                m_sendto(x,icons[n].name);
  141.                dprintf(stderr,"sent [%s] to %d\n",
  142.                        icons[n].name,x);
  143.                }
  144.             break;
  145.          case '^':        /* button down */
  146.             sscanf(line+1,"%d %d",&x,&y);
  147.             n = find_icon(icons,x,y);
  148.             if (n >= 0) {
  149.                border(icons[n],B_SET);
  150.                sprintf(line,"%s/%s",cwd,icons[n].name);
  151.                m_setevent(NOTIFY,line);
  152.                sprintf(line,"%s (%d x %d)", icons[n].name,
  153.             icons[n].w, icons[n].h);
  154.                pop_text(line, icons[n].x+icons[n].w/2,
  155.                                       icons[n].y+icons[n].h + GAP);
  156.                border(icons[n],B_CLEAR);
  157.                }
  158.             break;
  159.          case 'n':        /* next icons */
  160.                if ((first-argv) + count >= argc)
  161.                   break;
  162.                first += 2*count/3 + 1;
  163.                count = fill_page(first,icons);
  164.                set_menu(first-argv,argc-count-(first-argv)-1);
  165.                break;
  166.          case 'p':        /* previous icons */
  167.                if (first == argv)
  168.                    break;
  169.                first -= 2*count/3 + 1 ;
  170.                if (first < argv)
  171.                   first = argv;
  172.                count = fill_page(first,icons);
  173.                set_menu(first-argv,argc-count-(first-argv)-1);
  174.                break;
  175.          case 'M':        /* window moved */
  176.                get_size(&win_x,&win_y,&win_wide, &win_high);
  177.                break;
  178.          case 'r':        /* window  redrawn  */
  179.                count = fill_page(first,icons);
  180.                set_menu(first-argv,argc-count-(first-argv)-1);
  181.             break;
  182.          case 'R':        /* window shaped*/
  183.                dprintf(stderr,"Got %d,%d %dx%d [%dx%d]\n",
  184.                        win_x, win_y, win_wide, win_high, f_wide, f_high);
  185.                x = win_wide;
  186.                y = win_high;
  187.                get_size(&win_x,&win_y,&win_wide, &win_high);
  188.                if (y != win_high || x>win_wide) {
  189.                   count = fill_page(first,icons);
  190.                   set_menu(first-argv,argc-count-(first-argv)-1);
  191.                   }
  192.             break;
  193.          default:        /* button up (let go of button too fast) */
  194.             break;
  195.          }
  196.       }
  197.    clean(0);
  198.    }
  199.  
  200. int
  201. fill_page(names,icon)
  202. char **names;
  203. struct icon_pos *icon;
  204.    {
  205.    register int count = 0;
  206.    int x=GAP, y=GAP;            /* current icon position */
  207.    int w,h;                /* current icon size */
  208.    int maxh = 0;            /* max y extent of icons */
  209.  
  210.    if (invert)
  211.       m_func(B_COPYINVERTED);
  212.    else
  213.       m_func(B_COPY);
  214.    m_clear();
  215.    for(;*names;names++) {
  216.       if (**names == '/')
  217.          m_bitfile(1,*names, &w, &h);
  218.       else {
  219.          sprintf(line,"%s/%s",cwd,*names);
  220.          m_bitfile(1,line, &w, &h);
  221.          }
  222.       dprintf(stderr,"getting %s -> %s",*names,line);
  223.       if (w==0 || h == 0)
  224.          continue;
  225.  
  226.       if (w + x + 2*GAP > win_wide) {
  227.          x = GAP;
  228.          y += maxh + GAP;
  229.          maxh = 0;
  230.          }
  231.  
  232.       if (y + h + GAP > win_high) {
  233.          dprintf(stderr,"%s won't fit\n",*names);
  234.          break;
  235.          }
  236.  
  237.       m_movecursor(x,y);
  238.       m_bitcopyto(x,y,w,h,0,0,0,1);
  239.       count++;
  240.       icon->x = x;
  241.       icon->y = y;
  242.       icon->w = w;
  243.       icon->h = h;
  244.       icon->name = *names;
  245.  
  246.       icon++;
  247.       x += w + GAP;
  248.       maxh = h>maxh ? h : maxh;
  249.       }
  250.    icon->x = -1;
  251.    m_movecursor(0,win_high-f_high-GAP);
  252.    return(count);
  253.    }
  254.  
  255. clean(code)
  256. int code;
  257.    {
  258.     m_bitdestroy(1);
  259.    m_pop();
  260.    m_ttyreset();
  261.    exit(code);
  262.    }
  263.  
  264. /* border an icon */
  265.  
  266. border(icon,how)
  267. struct icon_pos icon;
  268. int how;
  269.    {
  270.    int x=icon.x, y=icon.y, w=icon.w, h=icon.h;
  271.    dprintf(stderr,"border: %d,%d %dx%d\n",x,y,w,h);
  272.  
  273.    m_func(how);
  274.    m_bitwrite(x-GAP,y-GAP,w+2*GAP,GAP);
  275.    m_bitwrite(x-GAP,y+h,w+2*GAP,GAP);
  276.    m_bitwrite(x-GAP,y,GAP,h);
  277.    m_bitwrite(x+w,y,GAP,h);
  278.    }
  279.    
  280. /* find an icon */
  281.  
  282. int
  283. find_icon(icon,x,y)
  284. register struct icon_pos *icon;
  285. register int x,y;
  286.    {
  287.    register int i = 0;
  288.  
  289.    dprintf(stderr,"Looking for: %d %d [%d]\n",x,y);
  290.  
  291.    for(;icon->x != -1;i++,icon++)  {
  292.       if (y>icon->y && x>icon->x &&
  293.                        y < icon->y + icon->h &&
  294.                        x < icon->x + icon->w) {
  295.          dprintf(stderr,"found %d\n",i);
  296.          return(i);
  297.          }
  298.       }
  299.    return(-1);
  300.    }
  301.    
  302. pop_text(s,x,y)
  303. char *s;        /* text to display */
  304. int x,y;        /* center of window */
  305.    {
  306.    int wide = (strlen(s)<5?5:strlen(s)) * f_wide + 10;
  307.    int high = f_high + 4*GAP;
  308.    int x0 = x - wide/2;
  309.    int y0 = y + f_high;
  310.  
  311.    if (x0<0)
  312.       x0 = GAP;
  313.    if (x0+wide > win_wide)
  314.       x0 = win_wide-wide-GAP;
  315.    if (y0+high > win_high - f_wide)
  316.       y0 = GAP;
  317.  
  318.    if (x0 < 0 || y0 + high > win_high - f_wide) {    /* not enough room */
  319.       m_gets(line);
  320.       return(1);
  321.       }
  322.  
  323.    /* save current window text */
  324.  
  325.    m_func(B_SRC);
  326.    m_bitcopyto(0,0,wide,high,x0,y0,SAVE,0);
  327.  
  328.    /* draw border */
  329.  
  330.    m_func(B_SET);
  331.    m_bitwrite(x0,y0,wide,high);
  332.    m_func(B_CLEAR);
  333.    m_bitwrite(x0+GAP,y0+GAP,wide-2*GAP,high-2*GAP);
  334.    m_moveprint(x0+2*GAP,y0+(high-f_high)/2+GAP+f_high,s);
  335.    m_movecursor(win_wide,y);
  336.  
  337.    m_gets(line);
  338.  
  339.    /* restore data */
  340.  
  341.    m_func(B_SRC);
  342.    m_bitcopyto(x0,y0,wide,high,0,0,0,SAVE);
  343.    m_bitdestroy(SAVE);
  344.    }
  345.  
  346. int
  347. set_menu(front,back)
  348. int front;
  349. int back;
  350.    {
  351.    register int i;
  352.    if (front>0 && back>0)
  353.       i = 1;
  354.    else if (front > 0)
  355.       i = 3;
  356.    else if (back > 0)
  357.       i = 2;
  358.    else
  359.       i = 4;
  360.  
  361.    m_selectmenu(i);
  362.    dprintf(stderr,"set menu %d (%d,%d)\n",i,front,back);
  363.    return(i);
  364.    }
  365.